// index.js
// const app = getApp()

const innerAudioContext = wx.createInnerAudioContext({
  useWebAudioImplement: true
})
const innerAudioContext1 = wx.createInnerAudioContext({
  useWebAudioImplement1: true
})
const list1 = ['🐞', '🐛', '🦋', '🕷️', '🐝', '🐜', '🐌', '🦗', '🦂', '🕸️']
const list2 = ['🎷', '🎻', '🎸', '🥁', '🎹', '🎺', '📞', '📠', '☎', '📱']
const list3 = ['🌷', '🌻', '🌼', '💐', '🌸', '🌹', '🌺', '🥀', '🥦', '⚜︎']
const list6 = ['👮‍♂️', '👷', '👨‍🍳', '👨‍🚀', '👩‍⚕️', '👰', '👨🏾‍⚕️', '👮🏿‍♀️', '🙅', '👨‍🍳']
const list5 = ['⚽', '🏈', '⚾', '⛳', '🏀', '🏐', '🎳', '🏓', '🏸', '🥌']
const list4 = ['🚘', '🚕', '🛵', '🚐', '🚚', '🚓', '🚆', '🚲︎', '🚑︎', '🚒', '🚌', '🛴', '🏍️']

const list7 = ['🐀', '🐂', '🐯', '🐰', '🐉', '🐍', '🐎', '🐐', '🐒', '🐓', '🐄', '🐎', '🐘', '🐈', '🦁', '🐕', '🦇', '🐾']
const list11 = ['🚂', '🚅', '🚜', '🚔', '🚈', '🚑', '⛽', '🚥', '🚲', '🚨']
const list9 = ['🍇', '🍌', '🍐', '🍉', '🍍', '🍓', '🍒', '🍎', '🍅', '🍋', '✂', '📐', '📎', '🔐', '🔑', '🏆', '🏅', '🥇', '🥈', '']

const list8 = ['🐕', '🐖', '🐬', '🐟', '🐋', '🦈', '🐠', '🐙', '🐚', '🐳', '🐡', '🐸']
const list10 = ['🌱', '🌵', '🍁', '🍄', '🌾', '🍂', '🌲', '🌴', '🌳', '🌴']
const list15 = ['🍆', '🥔', '🥒', '🥜', '🥕', '🌽', '🥦', '🌰', '🥑', '🍅']
const list13 = ['🍦', '🎂', '🍬', '🍭', '🍨', '🍩', '🥧', '🍫', '🍪', '🍧']
const list14 = ['🥢', '🏺', '🍴', '🥄', '🔪', '💎', '📯', '🔊', '📻', '🎥']
const list12 = ['🚟', '🚡', '📻', '🎥', '✈', '🛫', '🛬', '🚁', '🚀', '🛸', ]
const list16 = ['🎯',  '🥔', '🥒','🎲', '🔫', '🎱', '🎰', '🎮', '🃏', '🀄', '♟']
const list17 = ['🍦', '🎂', '🍬', '🍭', '🍨', '🍩', '🥧', '🍫', '🍪', '🍧','👓', '👔', '🧥', '👕', '🧦', '👖', '👗', '🧣', '🧤', '👚', '👢', '👞', '👒', '🎩']
const list18 = ['🐓', '🐤', '🦆', '🐦', '🦉', '🦅', '🐣', '🐥', '🦃', '🐔']

const list19 = ['🐊', '🐉', '🐢', '🦕', '🦎', '🦖', '🐍', '🐲']
let list_liwu = [list1, list2, list3, list4, list5, list6, list7, list8, list9, list10, list11, list12]
let guan = 1 //  第几关
let guan_k = [10, 8, 6, 4, 2, 0] // 每关对应错误次数
let liwu = 0 // 礼物序号
let listAll = [] // 全部礼物
let list_db = [] // 单音对比通关礼物
let m = '' // 临时按键
let num = [] // 产生的随机数，原音
let anj_arr = []
let jiance = false
let fangshi =''
Page({
  data: {
    cheskList: ['1\nC', '2\nD', '3\nE', '4\nF', '5\nG', '6\nA', '7\nB', '1\nC'],
    eve: 'r-t', // 选择类型
    jz_tingyin: true,
    jz_jianpan: false, // true键盘禁用
    jz_genghuan: true,
    jz_queding: true,
    listdis: [], // 正确图标列表
    yc_quanbu: false, // 隐藏键盘
    list_c: [], // 错误图标列表
    guan_date: '弹奏练习',
    guan_k: guan_k,
    n1: '',
    n2: '',
    n3: '',
    n4: '',
    n5: '',
    n6: '',
    n7: '',
    n8: '',
    shur_num:0,
    tu_size:"100rpx",
    scr_yinfu:'',
    hidden:false,
    list_db:[]

   
  },
  // 按键按下  r-d单音对比  r-l>单音选择 r-dd>多音对比r-ll">多音选择
  play1(e) {
    m =parseInt(e.currentTarget.id) 
    if (this.data.eve == 'r-l') {
      this.danyin_pd(e.currentTarget.id, guan - 1)

    } else if(this.data.eve == 'r-d' | this.data.eve == 'r-t' ) {
      this.huanyuan_jianpan()
      this.biaojipan(m)
      
      this.plays(m);
      this.setData({jz_queding:false})
    }else if(this.data.eve == 'r-dd' & jiance) {
      this.biaojipan(m)
      this.plays(m);
      
      anj_arr = anj_arr.concat(m)
      this.setData({shur_num:anj_arr.length})
      if (anj_arr.length==guan) {
        this.setData({jz_queding:false})
      } else {
        this.setData({jz_queding:true})
      }
    }else if(this.data.eve == 'r-ll' & jiance) {
      this.biaojipan(m)
      anj_arr = anj_arr.concat(m)
      this.setData({shur_num:anj_arr.length})
      if (anj_arr.length==guan) {
        this.duoyin_d_pd()
      } 
    }

    // console.log(num);
  },
  // 切换游戏种类 r-d单音对比  r-l>单音选择 r-dd>多音对比r-ll">多音选择          

  change(e) {
    fangshi = e.detail['value'];
  
    let guan = wx.getStorageSync(fangshi);  // 读取手机里保存的数值
    // 保存关级数值 wx.setStorageSync(fangshi, 1);
    if( !guan){guan=1};
    
    console.log(guan)
    anj_arr=[]
    
    this.setData({
      eve: fangshi, // 选择类型
      listdis: [], // 正确图标列表
      list_c: [], // 错误图标列表
      yc_quanbu: false, // 隐藏键盘
      guan_date: guan,
      guan_k: guan_k,
      n1: '',
      n2: '',
      n3: '',
      n4: '',
      n5: '',
      n6: '',
      n7: '',
      n8: '',
      shur_num:0,
      yincang:false
    });
    
    if (fangshi == 'r-t') {  // 弹奏练习
      this.setData({
        jz_tingyin: true,
        jz_jianpan: false, // true键盘禁用
        jz_genghuan: true,
        jz_queding: true,
        guan_date: '弹奏练习',
      });
    } else {
      this.setData({
        jz_tingyin: false,
        jz_jianpan: true, // true键盘禁用
        jz_genghuan: true,
        jz_queding: true,
        
      });
      if(fangshi == 'r-t1'){this.setData({guan_date: '听音练习',})}  // 听音练习
    }
    num = []
    liwu = 0;
   
    for (let i = 0; i < guan; i++) {    
      num = num.concat(Math.floor(Math.random() * 8 + 1));      //产生随机数
    }


  },

  // 单音判断 一共6关，
  danyin_pd(e, g) {
    if (e == num[0]) { // 正确了     
      this.setData({
        listdis: this.data.listdis.concat(list_liwu[g][liwu]), // 增加一个显示成绩
        jz_queding: true,
        jz_jianpan:true

      });
      this.huanyuan_jianpan()
      num[0] = Math.floor(Math.random() * 8 + 1);
      liwu += 1
      if (this.data.listdis.length == 10) {
        let that = this
        if (guan == 6) {                            // 通关了
          if (that.data.eve == 'r-d') {
            that.plays('枫叶城1');
            this.setData({
              list_db : list_db,
              yincang:true
            })
            

          } else {
            that.plays('枫叶城2');
            that.setData({
              list_db: listAll,
              yincang: true
            });
          }
          wx.showToast({
            title: '恭喜你！通关了！🏆',
            icon: 'success',
            duration: 3000
          })

        } else {
          
          this.plays('胜利');
          wx.showModal({
            content: '恭喜你！过关了，是否继续下一关？',
            success(res) {
              if (res.confirm) { //选择确定
                guan += 1
                wx.setStorageSync(fangshi,guan);
                console.log(guan)
                that.setData({guan_date: guan})
              } else if (res.cancel) {}; //选择取消
            }
          })
          that.huanyuan_jianpan()
          that.setData({
            jz_tingyin: false,
            jz_jianpan: true, // true键盘禁用
            jz_genghuan: true,
            jz_queding: true,

            list_c: [],
            listdis: [],
            
          });
          liwu = 0
          console.log('hhhhhh',this.data.guan_date,guan);
        };
      } else {
        this.plays('正确1');
      };

    } else { // 错误
      this.setData({
        list_c: this.data.list_c.concat('❌')
      });
      if (this.data.list_c.length >= guan_k[guan - 1]) {
        wx.showToast({
          title: '你失败了！',
          icon: 'error',
          duration: 3000,
        });
        this.plays('结束');
        this.huanyuan_jianpan()
        this.setData({
          jz_tingyin: false,
          jz_jianpan: true, // true键盘禁用
          jz_genghuan: true,
          jz_queding: true,

          list_c: [],
          listdis: [],
          guan_date: guan
        });
        liwu = 0
      } else {
        this.plays('失败1');

      };

    };
    
  },
  // 多音判断 没有关底，随着过关音符数量不断增加。
  duoyin_d_pd(auto_pd){
    console.log(anj_arr.toString(), num.toString());
    if (anj_arr.toString()== num.toString() ) {
      guan +=1
      wx.setStorageSync(fangshi,guan);
      anj_arr=[]
      this.setData({
        listdis: this.data.listdis.concat(list17[liwu]), // 增加一个显示成绩
        // jz_queding: true,
        // jz_jianpan:true,
        // guan_date:guan,
        // shur_num:anj_arr.length,
        tu_size:guan>10? '70rpx' : '100rpx'
      });
      liwu +=1
      num=[]
      for (let i = 0; i < guan; i++) {
        num = num.concat(Math.floor(Math.random() * 8 + 1));
      }
      this.plays('正确1');
      setTimeout(() => {
        this.setData({
          // listdis: this.data.listdis.concat(list17[liwu]), // 增加一个显示成绩
          jz_queding: true,
          jz_jianpan:true,
          guan_date:guan,
          shur_num:anj_arr.length,
          // tu_size:guan>10? '70rpx' : '100rpx'
        });
      }, 800);
    } else {
      anj_arr=[]
      
      this.plays('失败1');
      setTimeout(() => {
        this.setData({shur_num:anj_arr.length})
      }, 800);
    }
    setTimeout(() => {
      this.huanyuan_jianpan()
    }, 800);
    
  },
  // 点击确定  r-d单音对比  r-l>单音选择 r-dd>多音对比r-ll">多音选择
  queding() {
    if (this.data.eve=='r-d') {
      console.log(guan)
      this.danyin_pd(m, guan + 5)

    } else if (this.data.eve=='r-dd') {
      this.duoyin_d_pd()
      this.huanyuan_jianpan()
      
    } else {
      
    }
    

  },

   // 听音按钮  r-d单音对比  r-l>单音选择 r-dd>多音对比r-ll">多音选择
  tingying() {     
    if (this.data.eve == 'r-t1') {
      var suiji_n = Math.floor(Math.random() * 8 + 1)
      this.setData({
        jz_jianpan: true
      })
      this.huanyuan_jianpan()
      this.biaojipan(suiji_n);
      this.plays(suiji_n);
    } else if (this.data.eve == 'r-d') {
      this.setData({
        jz_jianpan: false,
        // jz_tingyin: true
      })
      this.plays(num[0]);
    } else if (this.data.eve == 'r-l') {
      this.setData({
        jz_jianpan: false,
        // jz_queding: false
      })
      this.plays(num[0]);
    }else if (this.data.eve == 'r-dd') {
      anj_arr=[]
      this.setData({
        jz_jianpan: true,
        jz_queding: true,
        jz_tingyin: true,
        shur_num:anj_arr.length
      })
      this.huanyuan_jianpan()
      this.play2(num, 0)
      
      this.setData({
        jz_jianpan: false,
        // jz_queding: false
      })
    }else if (this.data.eve == 'r-ll') {
      anj_arr=[]
      this.setData({
        jz_jianpan: true,
        jz_queding: true,
        jz_tingyin: true,
        shur_num:anj_arr.length
      })
      this.huanyuan_jianpan()
      this.play2(num, 0);   
      this.setData({
        jz_jianpan: false,
        jz_queding: false
      })
    }

  },
  plays(e) {
    this.xianshiyinfu(e)
    innerAudioContext.src = 'images/基本音级单音/' + e + '.mp3'
    innerAudioContext.stop() // 停止
    innerAudioContext.play() // 播放
    
  },
  play2(arr, currentIndex) {
    jiance =false
    this.xianshiyinfu(arr[currentIndex])
    innerAudioContext.stop() // 停止
    innerAudioContext1.src = 'images/基本音级单音/' + arr[currentIndex] + '.mp3';
    innerAudioContext1.play()

    setTimeout((e) => {
      innerAudioContext1.stop()
      
      currentIndex += 1
      if (currentIndex < arr.length ) {
        
        this.play2(arr, currentIndex)
      } else{
        jiance = true,
        this.setData({jz_tingyin: false,})
        
      }
      
    }, 800);

    
  },
  // 显示音符 
  xianshiyinfu(e){
    if(   parseInt(e) <9){
      console.log(parseInt(e))
      this.setData({
        scr_yinfu:'../../images/' + e + '.png'
      })
    }
    
    
    
  },
  // 标识按下的按键 
  biaojipan(e) {
    if (e == 1) {
      this.setData({
        n1: e,
      })
    } else if (e == 2) {
      this.setData({
        n2: e,
      })
    } else if (e == 3) {
      this.setData({
        n3: e,
      })
    } else if (e == 4) {
      this.setData({
        n4: e,
      })
    } else if (e == 5) {
      this.setData({
        n5: e,
      })
    } else if (e == 6) {
      this.setData({
        n6: e,
      })
    } else if (e == 7) {
      this.setData({
        n7: e,
      })
    } else if (e == 8) {
      this.setData({
        n8: e,
      })
    }
  },
  // 还原按键
  huanyuan_jianpan() {
    this.setData({
      n1: '',
      n2: '',
      n3: '',
      n4: '',
      n5: '',
      n6: '',
      n7: '',
      n8: '',
    });
  },
  // 显示五线谱选择框选择
  checkboxchange(e){
    if(e.detail.value[0]=='wuxian'){
      this.setData({hidden:true})
    }else{this.setData({hidden:false})}
  },
//返回第1关
fanhui(){
  guan=1;
  wx.setStorageSync(fangshi,guan);
  this.setData({guan_date:guan})
},


  // 页面初次加载
  onLoad() {
    listAll = [...list1, ...list2, ...list3, ...list4, ...list5, ...list6, ...list7, ...list8, ...list9, ...list10, ...list11, ...list12, ...list13, ...list14, ...list15, ...list16, ...list17, ...list18, ...list19]

    list_db = [...list7, ...list8, ...list9, ...list10, ...list11, ...list12, ]
    // list_db =['大师傅士大夫','的说法是','梵蒂冈']
    // this.setData({
    //   list_db : [...list7, ...list8, ...list9, ...list10, ...list11, ...list12, ],
    //   yincang:true
    // })

  },

 

  // 分享
  // onShareAppMessage:function () { 
  //   qq. showShareMenu({ 
  //     showShareItems:['qq',' qzone',' wechatFriends', ' wechatMoment ']
  //   })
  
  // },
  onShareAppMessage:function(){
    wx.showShareMenu({
      withShareTicket:true,
      menu:['shareAppMessage','shareTimeline']
    })
  },
  //用户点击右上角分享朋友圈
  onShareTimeline:function(){
    return {
      title:'',
      query:{
        key:value
      },
      imageUrl:''
    }
  },
  
});